<?php /* FORUMS $Id: index.php,v 1.35.6.3 2006/11/19 13:12:47 gregorerhardt Exp $ */
$AppUI->savePlace();

// retrieve any state parameters
if (isset( $_GET['orderby'] )) {
	$orderdir = $AppUI->getState( 'ForumIdxOrderDir' ) ? ($AppUI->getState( 'ForumIdxOrderDir' )== 'asc' ? 'desc' : 'asc' ) : 'desc';
	$AppUI->setState( 'ForumIdxOrderBy', $_GET['orderby'] );
	$AppUI->setState( 'ForumIdxOrderDir', $orderdir);
}
$orderby  = $AppUI->getState( 'ForumIdxOrderBy' ) ? $AppUI->getState( 'ForumIdxOrderBy' ) : 'forum_name';
$orderdir = $AppUI->getState( 'ForumIdxOrderDir' ) ? $AppUI->getState( 'ForumIdxOrderDir' ) : 'asc';

$perms =& $AppUI->acl();

$df = $AppUI->getPref( 'SHDATEFORMAT' );
$tf = $AppUI->getPref( 'TIMEFORMAT' );

$f = dPgetParam( $_POST, 'f', 0 );

$forum =& new CForum;
require_once $AppUI->getModuleClass('projects');
$project =& new CProject;

$max_msg_length = 30;

/* Query modified by Fergus McDonald 2005/08/12 to address slow join issue */

$q  = new DBQuery;
$q->addTable('forums');
$q->addTable('projects', 'p');
$q->addTable('users', 'u');
$q->addQuery('forum_id, forum_project, forum_description, forum_owner, forum_name');
$q->addQuery('forum_moderated, forum_create_date, forum_last_date');
$q->addQuery('sum(if(c.message_parent=-1,1,0)) AS forum_topics, SUM(IF(c.message_parent>0,1,0)) AS forum_replies');
$q->addQuery('user_id, user_username, project_name, project_color_identifier');
$q->addQuery('CONCAT(ct.contact_first_name, " ", ct.contact_last_name) AS contact_name');
$q->addQuery('SUBSTRING(l.message_body,1,'.$max_msg_length.') message_body');
$q->addQuery('LENGTH(l.message_body) message_length, watch_user, l.message_parent, l.message_id');
$q->addQuery('COUNT(DISTINCT v.visit_message) AS visit_count, count(DISTINCT c.message_id) AS message_count');
$q->addJoin('contacts', 'ct', 'u.user_contact = ct.contact_id');
$q->addJoin('forum_messages', 'l', 'l.message_id = forum_last_id');
$q->addJoin('forum_messages', 'c', 'c.message_forum = forum_id');
$q->addJoin('forum_watch', 'w', 'watch_user = '.$AppUI->user_id.' AND watch_forum = forum_id');
$q->addJoin('forum_visits', 'v', 'visit_user = '.$AppUI->user_id.' AND visit_forum = forum_id and visit_message = c.message_id');

$project->setAllowedSQL($AppUI->user_id, $q);
$forum->setAllowedSQL($AppUI->user_id, $q);


$q->addWhere("user_id = forum_owner AND project_id = forum_project");

switch ($f) {
	case 1:
		$q->addWhere("project_status <> 7 AND forum_owner = $AppUI->user_id");
		break;
	case 2:
		$q->addWhere("project_status <> 7 AND watch_user IS NOT NULL");
		break;
	case 3:
		$q->addWhere("project_status <> 7 AND project_owner = $AppUI->user_id");
		break;
	case 4:
		$q->addWhere("project_status <> 7 AND project_company = $AppUI->user_company");
		break;
	case 5:
		$q->addWhere("project_status = 7");
		break;
	default:
		$q->addWhere("project_status <> 7");
		break;
}

$q->addGroup('forum_id');
$q->addOrder("$orderby $orderdir");
$forums = $q->loadList();

// setup the title block
$titleBlock = new CTitleBlock( 'Forums' );
$titleBlock->addCell(
	arraySelect( $filters, 'f', 'size="1" class="text" onChange="document.forum_filter.submit();"', $f , true ), '',
	'<form name="forum_filter" action="?m=forums" method="post">', '</form>'
);

$canAdd = $perms->checkModule( $m, 'add');
if ($canAdd) {
	$titleBlock->addButton($AppUI->_('new forum'), 'js:doEditForum(0)');//'?m=forums&a=addedit');
}
$titleBlock->show();

if ($canAdd) {
?>
<script>
function doSaveForum() {
	var form = document.changeforum;
	if(form.forum_name.value.search(/^\s*$/) >= 0 ) {
		alert("<?=$AppUI->_('forumName', UI_OUTPUT_JS)?>");
		form.forum_name.focus();
	} else if(form.forum_project.selectedIndex < 1) {
		alert("<?=$AppUI->_('forumSelectProject', UI_OUTPUT_JS)?>");
		form.forum_project.focus();
	} else if(form.forum_owner.selectedIndex < 1) {
		alert("<?=$AppUI->_('forumSelectOwner', UI_OUTPUT_JS)?>");
		form.forum_owner.focus();
	} else {
		form.submit();
	}
}

function doEditForum(id) {
	ajax.send('index.php?m=forums&a=edit&suppressHeaders=1&forum_id='+id,'',
		function(r) {
			$('idEditForum').innerHTML = r;
			Dialog('idEditForum').show();
		}
	);
}
</script>
<? } ?>
<div id="idEditForum" autohide="click" class="popup" style="visibility: hidden">
</div>
<table width="100%" cellspacing="0" cellpadding="3" border="0" class="list">
<form name="watcher" action="index.php?m=forums&f=<?=$f?>" method="post">
<tr>
	<th width="32">&nbsp;</th>
	<th width="25"><a href="?m=forums&orderby=watch_user"><?=$AppUI->_( 'Watch' )?></a></th>
	<th><a href="?m=forums&orderby=forum_name"><?=$AppUI->_( 'Forums' )?></a></th>
	<th width="50" align="center"><a href="?m=forums&orderby=forum_topics"><?=$AppUI->_( 'Topics' )?></a></th>
	<th width="50" align="center"><a href="?m=forums&orderby=forum_replies"><?=$AppUI->_( 'Replies' )?></a></th>
	<th width="200"><a href="?m=forums&orderby=forum_last_date"><?=$AppUI->_( 'Last Post Info' )?></a></th>
</tr>
<?php
$p ="";
$now = new CDate();
foreach ($forums as $row) {
	$message_date = intval( $row['forum_last_date'] ) ? new CDate( $row['forum_last_date'] ) : null;

	if($p != $row['forum_project']) {
		$create_date = intval( $row['forum_create_date'] ) ? new CDate( $row['forum_create_date'] ) : null;
?>
<tr>
	<td colspan="6" onclick="go('?m=projects&a=view&project_id=<?=$row['forum_project']?>')" class="clickable">
		<img class="ico" src="images/projects.gif">&nbsp;<b><?=$row['project_name']?></b>
	</td>
</tr>
<?php
		$p = $row['forum_project'];
	}?>
<tr class="second">
	<td nowrap="nowrap" align="center">
	<?php if ( $row['forum_owner'] == $AppUI->user_id || $perms->checkModule('forums', 'add') ) { ?>
		<!--a href="?m=forums&a=addedit&forum_id=<?=$row['forum_id']?>" title="<?=$AppUI->_('Edit')?>"><img src="images/edit.gif"></a-->
		<a href="javascript:doEditForum(<?=$row['forum_id']?>)" title="<?=$AppUI->_('Edit')?>"><img src="images/edit.gif"></a>
<?php } 
if ( $row['visit_count'] != $row['message_count'] ) {
	echo '<img src="images/discuss.gif" title="You have unread messages in this forum">';
}
?>
	</td>
	<td nowrap="nowrap" align="center">
		<input type="checkbox" name="forum_<?=$row['forum_id']?>" <?=$row['watch_user'] ? 'checked' : ''?> />
	</td>
	<td onclick="go('?m=forums&a=viewer&forum_id=<?=$row['forum_id']?>')" class="clickable">
		<span style="font-size: 12px; font-weight: bold">
			<?=$row['forum_name']?>&nbsp;-&nbsp;
			<span style="color: #777; font-size: 70%; font-weight: normal">
			<?=$AppUI->_( 'Moderator' ).': '.$row['contact_name']?>&nbsp;-&nbsp;<?=$AppUI->_( 'since' ).' '.$create_date->format( $df )?></span></span>
		<br /><div style="padding-top: 8px"><?=$row['forum_description']?></div>
	</td>
	<td nowrap="nowrap" align="center"><?=$row['forum_topics']?></td>
	<td nowrap="nowrap" align="center"><?=$row['forum_replies']?></td>
	<td width="225">
<?php
if ($message_date !== null) {
	echo $message_date->format( "$df $tf" );

	$last = new Date_Span();
	$last->setFromDateDiff( $now, $message_date );

	echo '&nbsp;<font color=#999966>(';
	printf('%.1f', $last->format('%d'));
	echo '&nbsp;'.$AppUI->_('days ago') . ')</font>';

	$id = $row['message_parent'] < 0 ? $row['message_id'] : $row['message_parent'];

	echo '<br /><a href="?m=forums&a=viewer&forum_id='.$row['forum_id'].'&message_id='.$id.'">';
	echo '<img style="padding-top: 8px" class="ico" src="images/discuss_private.gif">&nbsp;';
	echo '<font color=#777777>'.$row['message_body'].($row['message_length'] > $max_msg_length ? '...' : '').'</font></a>';
} else {
	echo $AppUI->_('No posts');
}
?>
	</td>
</tr>

<?php } ?>
</table>

<table width="100%" cellspacing="1" cellpadding="0" border="0">
	<input type="hidden" name="dosql" value="do_watch_forum" />
	<input type="hidden" name="watch" value="forum" />
<tr>
	<td>&nbsp;</td>
</tr>
<tr>
	<td align="left">
		<input type="submit" class="button" value="<?=$AppUI->_( 'update watches' )?>" />
	</td>
</tr>
</form>
</table>
